package com.ruoyi.web.controller.biz;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ConvertUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.valid.ValidInsertGroup;
import com.ruoyi.common.valid.ValidUpdateGroup;
import com.ruoyi.system.domain.BsStrategy;
import com.ruoyi.system.domain.excel.BsStrategyExcel;
import com.ruoyi.system.domain.ro.BsStrategyRO;
import com.ruoyi.system.service.IBsStrategyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 攻略游记
 *
 * @author ruoyi
 * @date 2023-11-25
 */
@Validated
@RequiredArgsConstructor
@Api(tags = "WEB端-攻略游记")
@RestController
@RequestMapping("/system/strategy")
public class BsStrategyController extends BaseController {

    private final IBsStrategyService iBsStrategyService;

    /**
     * 查询攻略游记列表
     */
    @ApiOperation(value = "攻略游记-攻略游记列表导出", tags = {"WEB端-攻略游记"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "攻略标题"),
            @ApiImplicitParam(name = "auditStatus", value = "审核状态 0 已认证/待审核 1审核通过 2审核拒绝 详见字典strategy_audit_status"),
            @ApiImplicitParam(name = "beginCreateTime", value = "申请时间-start"),
            @ApiImplicitParam(name = "endCreateTime", value = "申请时间-end"),
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    @GetMapping("/export")
    public AjaxResult<?> export(@RequestParam(required = false) String title, @RequestParam(required = false) String auditStatus,
                                @RequestParam(required = false) String beginCreateTime, @RequestParam(required = false) String endCreateTime,
                                @RequestParam(required = false) String pageNum, @RequestParam(required = false) String pageSize) {
        BsStrategyRO ro = BsStrategyRO.builder().auditStatus(auditStatus).title(title).build();
        HashMap<String, Object> param = new HashMap<>(2);
        param.put("beginCreateTime", beginCreateTime);
        param.put("endCreateTime", endCreateTime);
        ro.setParams(param);
        List<BsStrategy> list = iBsStrategyService.queryPageList(ro);
        ExcelUtil<BsStrategyExcel> util = new ExcelUtil<>(BsStrategyExcel.class);
        return util.exportExcel(ConvertUtil.sourceToTarget(list, BsStrategyExcel.class), "攻略游记");
    }

    /**
     * 查询攻略游记列表
     */
    @ApiOperation(value = "攻略游记-攻略游记列表", tags = {"WEB端-攻略游记"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "攻略标题"),
            @ApiImplicitParam(name = "auditStatus", value = "审核状态 0 已认证/待审核 1审核通过 2审核拒绝 详见字典strategy_audit_status"),
            @ApiImplicitParam(name = "beginCreateTime", value = "申请时间-start"),
            @ApiImplicitParam(name = "endCreateTime", value = "申请时间-end"),
            @ApiImplicitParam(name = "pageNum", value = "分页参数：第几页"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数：每页条数"),
    })
    @GetMapping("/list")
    public Result<TableDataInfo<BsStrategy>> list(@RequestParam(required = false) String title, @RequestParam(required = false) String auditStatus,
                                                  @RequestParam(required = false) String beginCreateTime, @RequestParam(required = false) String endCreateTime,
                                                  @RequestParam(required = false) String pageNum, @RequestParam(required = false) String pageSize) {
        startPage();
        BsStrategyRO ro = BsStrategyRO.builder().auditStatus(auditStatus).title(title).build();
        HashMap<String, Object> param = new HashMap<>(2);
        param.put("beginCreateTime", beginCreateTime);
        param.put("endCreateTime", endCreateTime);
        ro.setParams(param);
        List<BsStrategy> list = iBsStrategyService.queryPageList(ro);
        return Result.success(getDataTable(list));
    }

    /**
     * 获取攻略游记详细信息
     */
    @ApiOperation(value = "攻略游记-攻略游记详情)", tags = {"WEB端-攻略游记"})
    @GetMapping("/{id}")
    public Result<BsStrategy> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long id) {
        return Result.success(iBsStrategyService.queryById(id));
    }

    /**
     * 获取攻略游记详细信息
     */
    @ApiOperation(value = "攻略游记-攻略游记审核", tags = {"WEB端-攻略游记"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "auditStatus", value = "审核状态 0 已认证/待审核 1审核通过 2审核拒绝 详见字典strategy_audit_status"),
            @ApiImplicitParam(name = "id", value = "攻略游记id"),
            @ApiImplicitParam(name = "auditReason", value = "拒绝原因（非必填）"),
    })
    @GetMapping("/audit")
    public Result<?> audit(@RequestParam() String auditStatus, @RequestParam Long id, @RequestParam(required = false) String auditReason) {
        BsStrategyRO ro = BsStrategyRO.builder().auditStatus(auditStatus).id(id)
                .auditBy(SecurityUtils.getLoginUser().getUser().getNickName())
                .auditTime(DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN))
                .auditReason(auditReason).build();
        return Result.toResult(iBsStrategyService.updateByBo(ro));
    }

    /**
     * 新增攻略游记
     */
    @Log(title = "攻略游记", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Deprecated
    public Result<?> add(@Validated(ValidInsertGroup.class) @RequestBody BsStrategyRO bo) {
        return Result.toResult(iBsStrategyService.insertByBo(bo));
    }

    /**
     * 修改攻略游记
     */
    @Log(title = "攻略游记", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Deprecated
    public Result<?> edit(@Validated(ValidUpdateGroup.class) @RequestBody BsStrategyRO bo) {
        return Result.toResult(iBsStrategyService.updateByBo(bo));
    }

    /**
     * 删除攻略游记
     *
     * @param ids 主键串
     */
    @ApiOperation(value = "攻略游记-攻略游记删除)", tags = {"WEB端-攻略游记"})
    @Log(title = "攻略游记", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<?> remove(@NotEmpty(message = "主键不能为空")
                            @PathVariable Long[] ids) {
        return Result.toResult(iBsStrategyService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
